<?php
declare (strict_types=1);

namespace app\admin\controller;


use app\common\model\MaterialCate;
use app\common\model\MaterialLibrary as MaterialLibraryModel;
use app\common\tool\Result;
use think\facade\View;
use think\Response;


class MaterialLibrary extends Base
{

    public function index(): Response
    {
        $param = $this->request->get();
        $that = MaterialLibraryModel::where(['status' => 1, 'is_del' => 0, 'cate' => $param['cate']])
            ->page(intval($param['page']), intval($param['limit']));
        if (isset($param['keyword']) && $param['keyword']) {
            $that->whereLike('title', '%' . $param['keyword'] . '%');
        }
        $count = MaterialLibraryModel::where(['status' => 1, 'is_del' => 0, 'cate' => $param['cate']])->count();
        return Result::fastReturn(['code' => 0, 'data' => $that->select(), 'count' => $count]);
    }

    public function open(): string
    {
        $cate = MaterialCate::where(['status' => 1, 'is_del' => 0])->field('id,title')->select();
        return View::assign([
            'cate'               => $cate,
            'allowSelectedCount' => $this->request->get('count', 1),
            'count'              => MaterialLibraryModel::where(['status' => 1, 'is_del' => 0, 'cate' => $cate[0]['id']])->count(),
            'id'                 => $this->request->get('id'),
            'name'               => $this->request->get('name')
        ])->fetch();
    }

    public function addGroup(): Response
    {
        return Result::fastReturn(MaterialCate::addGroup($this->request->post()));
    }

    public function destroy(): Response
    {
        try {
            $param = $this->request->post();
            $info = MaterialLibraryModel::where('id', $param['id'])->find();
            if (empty($info)) {
                return Result::fail('删除失败');
            }
            if (file_exists('.' . $info->url)) {
                unlink('.' . $info->url);
            }
            $info->delete();
            return Result::success('已删除');
        } catch (\Exception $e) {
            return Result::fail('删除失败：' . $e->getMessage());
        }
    }

    public function destroyGroup(): Response
    {
        try {
            $param = $this->request->post();
            $info = MaterialCate::where('id', $param['id'])->find();
            if (empty($info)) {
                return Result::fail('删除失败,分组不存在');
            }
            $list = MaterialLibraryModel::where('cate', $info->id)->select();
            foreach ($list as $k => $v) {
                if (file_exists('.' . $info->url)) {
                    unlink('.' . $v->url);
                }
                $v->delete();
            }
            $info->delete();
            return Result::success('已删除');
        } catch (\Exception $e) {
            return Result::fail('删除失败：' . $e->getMessage());
        }
    }

    public function saveGroup(): Response
    {
        MaterialCate::update($this->request->post());
        return Result::success('已修改');
    }

}
